public class TestDemo {

    // 递归求 N 的阶乘
    public static int fac(int n) {
        if (n == 1) {
            return 1;
        }
        return n * fac(n-1);
    }

    public static void main6(String[] args) {
        System.out.println(fac(5));
    }

    // 递归求和
    public static int summation(int n) {
        if (n == 1) {
            return n;
        }
        return n + summation(n-1);
    }

    public static void main5(String[] args) {
        System.out.println(summation(10));
    }

    // 递归打印数字的每一位
    public static void print(int n) {
        if (n < 10) {
            System.out.print(n + " ");
            return;
        }
        print(n/10);
        System.out.print(n%10 + " ");
    }

    public static void main4(String[] args) {
        print(1234);
    }

    // 返回的数字之和
//    public static int ret = 0;
//    public static int sum(int a) {
//        if (a < 10) {
//            return (ret+a);
//        }
//        ret += (a%10);
//        sum(a/10);
//        return ret;
//    }

    public static int sum(int a) {
        if (a < 10) {
            return a;
        }
        return a%10 + sum(a/10);
    }

    public static void main3(String[] args) {
        System.out.println(sum(154));
    }

    // 递归求斐波那契数列的第 N 项
    // 0,1,1,2,3,5,8,13,21,34,55
    public static int fibNum(int n) {
        if (n == 1) {
            return 0;
        }
        if (n == 2) {
            return 1;
        }
        n = fibNum(n-1) + fibNum(n-2);
        return n;
    }

    public static void main2(String[] args) {
        System.out.println(fibNum(1));
        System.out.println(fibNum(2));
        System.out.println(fibNum(20));
    }

    public static int max(int a, int b) {
        return a > b ? a : b;
    }

    public static double max(double a, double b, double c) {
        if (a >= b) {
            return a > c ? a : c;
        } else if (a < b) {
            return b > c ? b : c;
        }
        return -1;
    }

    public static void main1(String[] args) {
        int max1 = max(23, 71);
        double max2 = max(2.4, 1.68, 5.88);
        System.out.println(max1);
        System.out.println(max2);
    }
}
